package com.ly.fn.inf.config.ctx.utils;

import com.ly.fn.biz.ins.util.LangUtil;
import com.ly.fn.inf.config.ctx.conf.SystemEnv;
import com.ly.fn.inf.config.ctx.config.ConfigHome;
import com.ly.fn.inf.util.FileUtil;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ApplicationInitSettingUtils {
    public static final String APPLICATION_NAME = "app.name";
    public static final String APPLICATION_VERSION = "app.version";
    public static final String APP_CONFIG_FILE = "META-INF/app.props";
    public static final String APP_CONFIG_FILE_2 = "META-INF/app.properties";
    private static String app_name;
    private static ClassLoader classLoader;

    public ApplicationInitSettingUtils() {
    }

    public static String getApplicationProperty(ApplicationPropertyTypeEnum propertyType) {
        String return_value = null;
        if (classLoader == null) {
            classLoader = ClassUtils.getDefaultClassLoader();
        }

        switch (propertyType) {
            case APP_ENV_TYPE:
                return_value = getAppEnv(app_name == null ? getAppName() : app_name);
                break;
            case APP_VER_TYPE:
                return_value = getAppVersion();
                break;
            case APP_NAME_TYPE:
                return_value = getAppName();
                app_name = return_value;
        }

        return return_value;
    }

    private static String getAppName() {
        String appName = SystemEnv.getSystemId();
        Loggers.info("...Get App name [{}] from Class SystemEnv", new Object[]{appName});
        if (StringUtils.isEmpty(appName)) {
            appName = System.getProperty("app.name");
            Loggers.info("...Get App name [{}] from System Property..", new Object[]{appName});
        }

        if (StringUtils.isEmpty(appName)) {
            appName = System.getenv("app.name");
            Loggers.info("...Get App name [{}] from system Env...", new Object[0]);
        }

        if (StringUtils.isEmpty(appName)) {
            try {
                InputStream input = getInputStreamFromPropertyFile();
                if (input != null) {
                    Properties prop = new Properties();
                    prop.load(input);
                    appName = prop.getProperty("app.name");
                    Loggers.info("...Get App name [{}] from configuration file [{}]..", new Object[]{appName, "META-INF/app.props"});
                    prop = null;
                }
            } catch (IOException var3) {
                Loggers.error("... Get App name from config file {} found Exception:", new Object[]{"META-INF/app.props", var3});
            }
        }

        return appName;
    }

    private static String getAppVersion() {
        String version = System.getProperty("app.version");
        Loggers.info("...Get app version [{}] from System Property ...", new Object[]{version});
        if (StringUtils.isEmpty(version)) {
            version = System.getenv("app.version");
            Loggers.info("...Get app version [{}] from System Env ...", new Object[0]);
        }

        if (StringUtils.isEmpty(version)) {
            try {
                InputStream inputStream = getInputStreamFromPropertyFile();
                if (inputStream != null) {
                    Properties prop = new Properties();
                    prop.load(inputStream);
                    version = prop.getProperty("app.version");
                    Loggers.info("...Get app version [{}] from configuration file [{}]..", new Object[]{version, "META-INF/app.props"});
                    prop = null;
                }
            } catch (IOException var3) {
                Loggers.error("... Get App Version From Configuration File {} found Exception:", new Object[]{"META-INF/app.props", var3});
            }
        }

        return version;
    }

    private static String getAppEnv(String systemId){
        String env = SystemEnv.getEnv();
        Loggers.info("...Get app env [{}] From Class SystemEnv", new Object[]{env});
        if (StringUtils.isEmpty(env)) {
            if (StringUtils.isEmpty(systemId)) {
                systemId = app_name == null ? getAppName() : app_name;
            }

            if (StringUtils.isEmpty(systemId)) {
                throw new IllegalArgumentException("...The Parameter [systemId] is Empty...");
            }

            String config_home = ConfigHome.getDir();
            config_home = config_home + "/" + systemId;

            //自动检测本地配置文件目录
            String configDir = config_home;
            File directory = new File(configDir);
            if (!directory.exists()) {
                directory.mkdirs();
            }
            File envFile = new File(config_home + "/env");
            if (!envFile.exists()) {
                try {
                    envFile.createNewFile();
                    FileUtil.appendFile(envFile, LangUtil.defValue(env, "integration").getBytes());
                } catch (IOException ex) {

                }
            }

            env = FileUtil.readLine(envFile);
            Loggers.info("...Get env [{}] From local env file:{}", new Object[]{env, envFile.getAbsolutePath()});
        }

        return env;
    }

    private static InputStream getInputStreamFromPropertyFile() {
        InputStream inputStream = classLoader != null ? classLoader.getResourceAsStream("META-INF/app.properties") : ClassLoader.getSystemResourceAsStream("META-INF/app.properties");
        if (inputStream == null) {
            inputStream = classLoader != null ? classLoader.getResourceAsStream("META-INF/app.props") : ClassLoader.getSystemResourceAsStream("META-INF/app.props");
        }

        if (null == inputStream) {
            throw new IllegalArgumentException("...Please check your configuration File {META-INF/app.properties OR META-INF/app.props}");
        } else {
            return inputStream;
        }
    }
}
